package com.gexin.controller.system.product;

import com.gexin.common.WebConst;
import com.gexin.common.utils.StringUtil;
import com.gexin.core.web.SystemBaseController;
import com.gexin.model.product.ProductSkuKey;
import com.gexin.model.product.ProductSkuValue;
import com.gexin.model.product.Products;
import com.gexin.model.shop.ShopInfo;
import com.jfinal.ext.route.ControllerBind;
import com.jfinal.log.Log;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品的SKU操作
 * @author 肖得明
 *  2016-06-08
 *
 */
@ControllerBind(controllerKey = "/system/product/sku", viewPath = "WEB-INF/system/product")
public class ProductSKUController extends SystemBaseController {
	private static Log log = Log.getLog(ProductSKUController.class);
	public void index(){
		log.info("商品列表");
		Products product = getModel(Products.class, "product");
		paginate(Products.dao.paginate(ShopInfo.shop_id, product,getpage(), WebConst.Page_Size));
		setAttr("product", product);
		render("product_list.html");
	}
	//查询自己的sku keylist
	public void keylist(){
		List<ProductSkuKey>  list = ProductSkuKey.dao.getKeyByShopId(ShopInfo.shop_id);
		success(list);
	}
	//添加值
	//fang 2016-06-15 增加  key_id
	public void addvalue(){
		String  key_value = getPara("key_value");
		String key_id=getPara("key_id");
		String sp[] =key_value.split(",");
		Map<Object,String>  map =new HashMap<Object,String>();
		for(String str : sp){
			ProductSkuValue obj =new ProductSkuValue();
			obj.setValueName(str);
			obj.setKeyId(key_id);
			obj.save();
			map.put(obj.getValueId(), obj.getValueName());
		}
		success(map);
	}
	//添加一个SKU
	public void addkey(){
		ProductSkuKey key =new ProductSkuKey();
		String  key_name = getPara("key_name");
		if(StringUtil.isBlank(key_name)){
			fail("参数不能为空！");
			return;
		}
		key.setKeyName(key_name);
		key.setCreatorShopid(ShopInfo.shop_id);
		if(key.save()){
			success(key.getKeyId());
		}else{
			fail();
		}
	}
	
}


